package edu.yang;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class WebPatch {

    public static  String rootPath = "D:\\works\\i84bu";//web路径
    public static String desRoot ="D:\\i84\\temp\\package";
    public static String webApp="i84bu";//web应用文件夹名

//    public static  String rootPath = "A:\\workplace\\i84cpn\\i84cpn-timer";//定时器

    public static String patchFile= desRoot +"\\exportFile.txt";//补丁文件,由eclipse svn plugin生成

    public static String projectPath= rootPath + "\\src\\main";//项目文件夹路径



    public static String classPath= rootPath + "\\target\\classes";//class存放路径
//    public static String classPath= rootPath + "\\target\\i84cpn-timer-0.1\\WEB-INF\\classes";//class存放路径

    public static String desPath=desRoot+"//"+webApp;//补丁文件包存放路径

   // public static String version="i84cpn-web-0.1";//补丁版本
//    public static String version="i84cpn-timer-0.1";//补丁版本


    /**
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
//        List<String> fileList = filterPatternExcept("receiving", getPatchFileList());
//        copyFiles(fileList);
       copyFiles(getPatchFileList());

    }

    public static List<String> getPatchFileList() throws Exception{
        List<String> fileList=new ArrayList<String>();
        FileInputStream f = new FileInputStream(patchFile);
        BufferedReader dr=new BufferedReader(new InputStreamReader(f,"utf-8"));
        String line;
        while((line=dr.readLine())!=null){
            if(line.indexOf("main")!=-1){
                if(line.indexOf("application.properties") != -1){
                    System.out.println("！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！application.properties文件有修改请比对处理！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！");
                    continue;
                }
                line=line.substring(line.indexOf("main")+5,line.length());
                fileList.add(line);
            }else if(line.indexOf(".pom") != -1){
                System.out.println("！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！pom文件有修改请查看是否需要增加jar文件！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！！");
            }
        }
        return fileList;
    }

    /**
     * 过滤不需要上线的模块
     * @param packName
     * @return
     */
    public static List<String> filterPatternExcept(String packName, List<String> fileList){
        return fileList.stream().filter(p->!p.contains(packName)).collect(Collectors.toList());
    }

    /**
     * 过滤包含的模块
     * @param packageName
     * @param fileList
     * @return
     */
    public static List<String> filterPatternInclude(String packageName, List<String> fileList) {
        return fileList.stream().filter(p->p.contains(packageName)).collect(Collectors.toList());
    }

    public static void copyFiles(List<String> list){

        for(String fullFileName:list){
            if(fullFileName.indexOf("java/")!=-1){//对源文件目录下的文件处理
                String fileName=fullFileName.replace("java/", "/");
                fullFileName=classPath+fileName;
                if(fileName.endsWith(".java")){
                    fileName=fileName.replace(".java",".class");
                    fullFileName=fullFileName.replace(".java",".class");

                }
                String tempDesPath=fileName.substring(0,fileName.lastIndexOf("/"));
                String desFilePathStr=desPath+"/WEB-INF/classes"+tempDesPath;
                String desFileNameStr=desPath+"/WEB-INF/classes"+fileName;
                File desFilePath=new File(desFilePathStr);
                if(!desFilePath.exists()){
                    desFilePath.mkdirs();
                }
                copyFile(fullFileName, desFileNameStr);
                System.out.println(fullFileName+"复制完成");
            }else if(fullFileName.indexOf("resources/")!=-1) {//对resource文件处理
                String fileName=fullFileName.replace("resources/", "/");
                fullFileName=classPath+fileName;
                String tempDesPath=fileName.substring(0,fileName.lastIndexOf("/"));
                String desFilePathStr=desPath+"/WEB-INF/classes"+tempDesPath;
                String desFileNameStr=desPath+"/WEB-INF/classes"+fileName;
                File desFilePath=new File(desFilePathStr);
                if(!desFilePath.exists()){
                    desFilePath.mkdirs();
                }
                copyFile(fullFileName, desFileNameStr);
                System.out.println(fullFileName+"复制完成");
            }else{//对普通目录的处理
                String desFileName=fullFileName.replaceAll(webApp,"");
                fullFileName=projectPath+"/"+fullFileName;//将要复制的文件全路径
                String fullDesFileNameStr=desPath+desFileName;
                String desFilePathStr=fullDesFileNameStr.substring(0,fullDesFileNameStr.lastIndexOf("/"));
                File desFilePath=new File(desFilePathStr);
                if(!desFilePath.exists()){
                    desFilePath.mkdirs();
                }
                copyFile(fullFileName, fullDesFileNameStr);
                System.out.println(fullDesFileNameStr+"复制完成");
            }

        }

    }

    private static void copyFile(String sourceFileNameStr, String desFileNameStr) {
        File srcFile=new File(sourceFileNameStr);
        File desFile=new File(desFileNameStr);
        if(srcFile.isDirectory()){
            System.out.println(sourceFileNameStr+"目录文件不需要复制");
            return ;
        }
        try {
            copyFile(srcFile, desFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //处理有内部类的文件
        if(sourceFileNameStr.endsWith(".class")){
            final String fileName = sourceFileNameStr.substring(sourceFileNameStr.lastIndexOf("/")+1);
            String dir = sourceFileNameStr.substring(0, sourceFileNameStr.lastIndexOf("/")+1);
            File f = new File(dir);
            String[] files = f.list(new FilenameFilter() {
                @Override
                public boolean accept(File dir, String name) {
                    return name.startsWith(fileName.substring(0, fileName.indexOf(".class"))+"$");
                }
            });
            for(String fn : files){
                srcFile = new File(dir + fn);
                desFile=new File(desFileNameStr.substring(0, desFileNameStr.lastIndexOf("/")+1)+fn);
                try {
                    copyFile(srcFile, desFile);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    public static void copyFile(File sourceFile, File targetFile) throws IOException {
        BufferedInputStream inBuff = null;
        BufferedOutputStream outBuff = null;
        try {
            // 新建文件输入流并对它进行缓冲
            inBuff = new BufferedInputStream(new FileInputStream(sourceFile));

            // 新建文件输出流并对它进行缓冲
            outBuff = new BufferedOutputStream(new FileOutputStream(targetFile));

            // 缓冲数组
            byte[] b = new byte[1024 * 5];
            int len;
            while ((len = inBuff.read(b)) != -1) {
                outBuff.write(b, 0, len);
            }
            // 刷新此缓冲的输出流
            outBuff.flush();
        } finally {
            // 关闭流
            if (inBuff != null)
                inBuff.close();
            if (outBuff != null)
                outBuff.close();
        }
    }

}
